#include "Connection.hpp"

#include <iostream>
#include <iosfwd>
#include <iterator>
#include <locale>
#include <codecvt>

#include <cppsaxxmlparser.hpp>

using namespace std;

using namespace CppSaxXmlParser;

class XMLListener : public IXmlListener
{
public:
	virtual void onHeaderOpen(const Parser& parser)
	{};

	virtual void onHeaderClose(const Parser& parser)
	{};

	virtual void onTagOpen(const Parser& parser, const std::wstring& tagName)
	{};

	virtual void onTagClose(const Parser& parser, const std::wstring& tagName)
	{};

	virtual void onTagText(const Parser& parser, const std::wstring& tagText, const std::wstring& tagName)
	{};

	virtual void onAttribute(const Parser& parser, const std::wstring& attributeName, const std::wstring& attributeValue)
	{};

	virtual void onCDATA(const Parser& parser, const std::wstring& cdata){};
};

Connection::Connection()
{
}

Connection::~Connection()
{
}


void Connection::start()
{
	wbuffer_convert<codecvt_utf8<wchar_t>> conv(m_stream.rdbuf());

	wistream inputStream(&conv);

	XMLListener listener;

	Parser parser(listener, inputStream);
	parser.parse();

	listener.onHeaderOpen(parser);
}

boost::asio::ip::tcp::iostream& Connection::stream()
{
	return m_stream;
}
